from django.contrib import admin
from .models import Order, OrderItem, Cart, OrderLog


class OrderItemInline(admin.TabularInline):
    model = OrderItem
    extra = 0
    readonly_fields = ['flower_name', 'flower_image', 'spec_name', 'unit_price', 'quantity', 'total_price']
    can_delete = False


class OrderLogInline(admin.TabularInline):
    model = OrderLog
    extra = 0
    readonly_fields = ['action', 'description', 'operator', 'created_at']
    can_delete = False


@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
    list_display = [
        'order_no', 'user', 'status', 'receiver_name', 'actual_amount', 
        'payment_method', 'created_at'
    ]
    list_filter = ['status', 'payment_method', 'created_at', 'payment_time']
    search_fields = ['order_no', 'user__nickname', 'user__username', 'receiver_name', 'receiver_phone']
    ordering = ['-created_at']
    readonly_fields = ['order_no', 'created_at', 'updated_at']
    inlines = [OrderItemInline, OrderLogInline]
    
    fieldsets = (
        ('订单信息', {
            'fields': ('order_no', 'user', 'status', 'created_at', 'updated_at')
        }),
        ('收货信息', {
            'fields': ('receiver_name', 'receiver_phone', 'receiver_address')
        }),
        ('价格信息', {
            'fields': ('total_amount', 'discount_amount', 'shipping_fee', 'actual_amount')
        }),
        ('订单详情', {
            'fields': ('remark', 'delivery_time')
        }),
        ('支付信息', {
            'fields': ('payment_method', 'payment_time', 'transaction_id'),
            'classes': ('collapse',)
        }),
        ('物流信息', {
            'fields': ('shipping_company', 'tracking_number', 'shipped_time', 'delivered_time'),
            'classes': ('collapse',)
        })
    )
    
    def get_readonly_fields(self, request, obj=None):
        readonly_fields = list(self.readonly_fields)
        if obj:  # 编辑时
            readonly_fields.extend(['user', 'total_amount', 'actual_amount'])
        return readonly_fields
    
    actions = ['mark_as_paid', 'mark_as_shipped', 'mark_as_delivered']
    
    def mark_as_paid(self, request, queryset):
        updated = queryset.filter(status='pending').update(status='paid')
        self.message_user(request, f'成功标记 {updated} 个订单为已付款')
    mark_as_paid.short_description = '标记为已付款'
    
    def mark_as_shipped(self, request, queryset):
        updated = queryset.filter(status='paid').update(status='shipped')
        self.message_user(request, f'成功标记 {updated} 个订单为已发货')
    mark_as_shipped.short_description = '标记为已发货'
    
    def mark_as_delivered(self, request, queryset):
        updated = queryset.filter(status='shipped').update(status='delivered')
        self.message_user(request, f'成功标记 {updated} 个订单为已送达')
    mark_as_delivered.short_description = '标记为已送达'


@admin.register(OrderItem)
class OrderItemAdmin(admin.ModelAdmin):
    list_display = ['order', 'flower_name', 'spec_name', 'unit_price', 'quantity', 'total_price']
    list_filter = ['created_at']
    search_fields = ['order__order_no', 'flower_name']
    ordering = ['-created_at']
    readonly_fields = ['total_price', 'created_at']


@admin.register(Cart)
class CartAdmin(admin.ModelAdmin):
    list_display = ['user', 'flower', 'flower_spec', 'quantity', 'total_price', 'created_at']
    list_filter = ['created_at', 'updated_at']
    search_fields = ['user__nickname', 'user__username', 'flower__name']
    ordering = ['-created_at']
    readonly_fields = ['total_price', 'created_at', 'updated_at']


@admin.register(OrderLog)
class OrderLogAdmin(admin.ModelAdmin):
    list_display = ['order', 'action', 'description', 'operator', 'created_at']
    list_filter = ['action', 'created_at']
    search_fields = ['order__order_no', 'action', 'description', 'operator']
    ordering = ['-created_at']
    readonly_fields = ['created_at']